<?php

namespace App\Admin\Controllers;

use App\Models\Config;
use App\Models\Good;
use App\Models\Test;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Form\Field\Table;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class TestController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Good';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Test());
        $grid->disableExport();
        $grid->model()->orderBy('id', 'desc');



        $grid->column('id', __('Id'));

        $grid->column('img', __('商品图片'))->lightbox(['width' => 50, 'height' => 50]);

        $grid->column('created_at', __('创建时间'));
        //$grid->column('updated_at', __('Updated at'));


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Good::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('商品名称'));
        $show->field('product', __('规格属性'));
        $show->field('description', __('商品简介'));
        $show->field('img', __('商品图片'))->image();
        $show->field('price', __('价格'));
        $show->field('nable', __('标签'));
        $show->field('stock', __('库存'));
        $show->field('detail_img', __('详情'))->unescape();
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Test());



        $form->multipleFile('img', __('商品图片'))->removable();


        return $form;
    }


    public function send(Content $content)
    {
        //添加请求
        if (request()->isMethod('post')) {
            $data = request()->is_show;


            $res = DB::table('config')->where('name','is_show')->update(['value'=>$data]);

            if($res){
                $content->withSuccess('提醒', '操作成功');
                return redirect('/admin/show');
            }else{
                $content->withError('提醒', '操作失败');
                return redirect('/admin/show');
            }


        }


        $content->header('商品价格显示设置');

        $form = new \Encore\Admin\Widgets\Form();
        $form->setAction('show');
        $is_show = DB::table('config')->where('name','is_show')->value('value');

        $form->radio('is_show', __('价格是否显示'))->options(['1' => '是', '2'=> '否'])->default($is_show);


        $content->body($form);
        $js = <<<SCRIPT

SCRIPT;
        \Encore\Admin\Facades\Admin::script($js);
        return $content;

    }

}
